package com.example.firstdemo

import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.example.firstdemo.http.RetrofitClient
import com.example.firstdemo.model.LoginRequest
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class HttpActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_http)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        findViewById<Button>(R.id.getChannel).setOnClickListener {
            try {
                lifecycleScope.launch(context = Dispatchers.Main) {
                    val res = withContext(Dispatchers.IO) {
                        RetrofitClient.apiService.getChannels()
                    }
                    if (res.code == 10000) {
                        val data = res.data?.channels
                        findViewById<TextView>(R.id.channel).text =
                            "获取频道列表成功, 频道名称: ${data?.joinToString(", ") { it.name }}"
                    } else {
                        Toast.makeText(this@HttpActivity, "获取频道列表失败", Toast.LENGTH_SHORT)
                            .show()
                    }
                }
            } catch (error: Exception) {
                Toast.makeText(this@HttpActivity, "获取频道列表失败", Toast.LENGTH_SHORT).show()
            }
        }
        // 2. 登录操作
        findViewById<Button>(R.id.login).setOnClickListener {
            try {
                lifecycleScope.launch {
                    val req = LoginRequest("admin", "123456")
                    val res = withContext(Dispatchers.IO) {
                        RetrofitClient.apiService.login(req)
                    }

                    if (res.code == 10000) {
                        val token = res.data?.token
                        findViewById<TextView>(R.id.tv).text = "登录成功, Token: $token"
                    } else {
                        // 处理响应
                        Toast.makeText(
                            this@HttpActivity,
                            "登录失败",
                            Toast.LENGTH_SHORT
                        ).show()
                    }

                }
            } catch (e: Exception) {
                // 处理错误
                Toast.makeText(
                    this@HttpActivity,
                    "请求失败",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

    }
}